<template>
  <div>
    <HandleButton tip-title="审核" @click="getAuditInfo"/>
    <el-dialog
        title="商品审核"
        :visible.sync="dialogVisible"
        width="50%"
        @close="dialogVisible = false">
      <div>
        <AuditInfo :audit-info="auditInfo"/>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="auditReject">拒绝</el-button>
        <el-button type="primary" @click="auditPass">通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { MessageBox } from 'element-ui';
import {toExamineProduct} from "@/network/goods"
import HandleButton from "@/components/common/buttons/Handle"
import AuditInfo from "@/components/goods/auditChildComponents/auditInfo";
import {getAuditProductInfo} from "@/network/goods"
export default {
  name: "auditHandle",
  components: {HandleButton,AuditInfo},
  props: ['goodsInfo'],
  data(){
    return {
      dialogVisible: false,
      auditInfo: {}
    }
  },
  methods: {
    /*管理员点击通过按钮，审核通过*/
    auditPass(){
      this.auditOver({
        remark: "",
        status: 1, // -1表示未审核，0表示不通过，1表示通过
      })
    },
    /*管理员点击拒绝，审核没有通过*/
    auditReject(){
      MessageBox.prompt('请输入拒绝理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[a-zA-Z\d\u4e00-\u9fa5_/]+$/,
        inputErrorMessage: '非法字符'
      }).then(text=>{
        this.auditOver({
          remark: text.value,
          status: 0, // -1表示未审核，0表示不通过，1表示通过
        })
      })

    },
    /**
     * @function 审核农户初级农产品
     * @param obj {Object}
     * @param obj.remark {String} 备注
     * @param obj.status {Number} 审核状态
     * @return null
     * */
    async auditOver(obj) {
      const {remark,status} = obj
      const {id,productId} = this.goodsInfo
      const {userId} = this.auditInfo
      const {code, msg} = await toExamineProduct(productId,id,remark,status,userId)
      if (code !== 200) return this.$errorMessage(msg)
      this.dialogVisible = false
      this.$emit("changeType",id)
    },
    /*获取审核详细信息*/
    async getAuditInfo() {
      this.dialogVisible = true
      const {productId:id} = this.goodsInfo
      const {data} = await getAuditProductInfo(id,0)
      this.auditInfo = data
      // console.log(msg,data,code)
    },

  }
}
</script>

<style scoped>

</style>
